# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import datetime

import openpyxl


class GwdangPipeline:
    def __init__(self):
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.create_sheet('sku', 0)
        self.ws.append(('sku', '描述', '价格'))

    def close_spider(self, spider):
        self.wb.save("轻薄本{}.xlsx".format(datetime.date.today()))

    def process_item(self, item, spider):
        sku = item.get('id', '')
        desc = item.get('desc', '')
        price = item.get('price', '')
        self.ws.append((sku, desc, price))
        return item
